package org.light.generator;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Independent;
import org.light.domain.Statement;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class MvcConfigJavaGenerator extends Independent{
	private static final long serialVersionUID = -9171617426302332622L;

	public MvcConfigJavaGenerator(){
		super();
		this.fileName = "MvcConfig.java";
		this.standardName = "MvcConfig";
	}
	
	public MvcConfigJavaGenerator(String packageToken){
		super();
		this.packageToken = packageToken;
		this.fileName = "MvcConfig.java";
		this.standardName = "MvcConfig";
	}
	
	@Override
	public void setPackageToken(String packageToken){
		this.packageToken = packageToken+"";
	}
	
	@Override
	public String generateImplString() {
		List<Writeable> sList = new ArrayList<Writeable>();
		if (!StringUtil.isBlank(this.packageToken)) {
			sList.add(new Statement(1000L,0,"package "+this.packageToken+";"));
			sList.add(new Statement(2000L,0,""));
		}
		sList.add(new Statement(3000L,0,"import java.util.Arrays;"));
		sList.add(new Statement(4000L,0,"import java.util.List;"));
		sList.add(new Statement(5000L,0,""));
		sList.add(new Statement(6000L,0,"import org.springframework.boot.SpringBootConfiguration;"));
		sList.add(new Statement(7000L,0,"import org.springframework.context.annotation.Bean;"));
		sList.add(new Statement(8000L,0,"import org.springframework.web.cors.CorsConfiguration;"));
		sList.add(new Statement(9000L,0,"import org.springframework.web.cors.UrlBasedCorsConfigurationSource;"));
		sList.add(new Statement(10000L,0,"import org.springframework.web.filter.CorsFilter;"));
		sList.add(new Statement(11000L,0,"import org.springframework.web.servlet.config.annotation.CorsRegistry;"));
		sList.add(new Statement(12000L,0,"import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;"));
		sList.add(new Statement(13000L,0,""));
		sList.add(new Statement(14000L,0,"@SpringBootConfiguration"));
		sList.add(new Statement(15000L,0,"public class MvcConfig implements WebMvcConfigurer{"));
		sList.add(new Statement(16000L,0,""));
		sList.add(new Statement(17000L,0,"//	@Override"));
		sList.add(new Statement(18000L,0,"//"));
		sList.add(new Statement(19000L,0,"//	public void addCorsMappings(CorsRegistry registry) {"));
		sList.add(new Statement(20000L,0,"//"));
		sList.add(new Statement(21000L,0,"//// 这里匹配了所有的URL，允许所有的外域发起跨域请求，允许外域发起请求任意HTTP Method，允许跨域请求包含任意的头信息。"));
		sList.add(new Statement(22000L,0,"//"));
		sList.add(new Statement(23000L,0,"//		registry.addMapping(\"/**\")"));
		sList.add(new Statement(24000L,0,"//"));
		sList.add(new Statement(25000L,0,"//				.allowedHeaders(\"*\")"));
		sList.add(new Statement(26000L,0,"//"));
		sList.add(new Statement(27000L,0,"//				.allowedMethods(\"*\")"));
		sList.add(new Statement(28000L,0,"//"));
		sList.add(new Statement(29000L,0,"//				.allowedOrigins(\"*\")"));
		sList.add(new Statement(30000L,0,"//"));
		sList.add(new Statement(31000L,0,"//				.allowCredentials(true);"));
		sList.add(new Statement(32000L,0,"//"));
		sList.add(new Statement(33000L,0,"//	}"));
		sList.add(new Statement(34000L,1,""));
		sList.add(new Statement(35000L,1,"@Bean"));
		sList.add(new Statement(36000L,1,"public CorsFilter corsFilter() {"));
		sList.add(new Statement(37000L,0,""));
		sList.add(new Statement(38000L,2,"//创建CorsConfiguration对象后添加配置"));
		sList.add(new Statement(39000L,2,"CorsConfiguration config = new CorsConfiguration();"));
		sList.add(new Statement(40000L,2,""));
		sList.add(new Statement(41000L,2,"List<String> allowedHeaders = Arrays.asList(\"x-token\", \"content-type\", \"X-Requested-With\", \"XMLHttpRequest\");"));
		sList.add(new Statement(42000L,2,"List<String> exposedHeaders = Arrays.asList(\"x-token\", \"content-type\", \"X-Requested-With\", \"XMLHttpRequest\","));
		sList.add(new Statement(43000L,4,"\"Referrer Policy:no-referrer-when-downgrade\");"));
		sList.add(new Statement(44000L,2,"List<String> allowedMethods = Arrays.asList(\"POST\", \"GET\", \"DELETE\", \"PUT\", \"OPTIONS\");"));
		sList.add(new Statement(45000L,2,"List<String> allowedOrigins = Arrays.asList(\"*\");"));
		sList.add(new Statement(46000L,2,"config.setAllowedHeaders(allowedHeaders);"));
		sList.add(new Statement(47000L,2,"config.setAllowedMethods(allowedMethods);"));
		sList.add(new Statement(48000L,2,"config.setAllowedOrigins(allowedOrigins);"));
		sList.add(new Statement(49000L,2,"config.setExposedHeaders(exposedHeaders);"));
		sList.add(new Statement(50000L,2,"config.setMaxAge(36000L);"));
		sList.add(new Statement(51000L,2,"config.setAllowCredentials(true);"));
		sList.add(new Statement(52000L,0,"//        //设置放行哪些原始域"));
		sList.add(new Statement(53000L,0,"//        config.addAllowedOrigin(\"*\");"));
		sList.add(new Statement(54000L,0,"//"));
		sList.add(new Statement(55000L,0,"//        //放行哪些原始请求头部信息"));
		sList.add(new Statement(56000L,0,"//        config.addAllowedHeader(\"*\");"));
		sList.add(new Statement(57000L,0,"//        //暴露哪些头部信息"));
		sList.add(new Statement(58000L,0,"//        config.addExposedHeader(\"*\");"));
		sList.add(new Statement(59000L,2,"//放行哪些请求方式"));
		sList.add(new Statement(60000L,0,"//        config.addAllowedMethod(\"GET\");     //get"));
		sList.add(new Statement(61000L,0,"//        config.addAllowedMethod(\"PUT\");     //put"));
		sList.add(new Statement(62000L,0,"//        config.addAllowedMethod(\"POST\");    //post"));
		sList.add(new Statement(63000L,0,"//        config.addAllowedMethod(\"DELETE\");  //delete"));
		sList.add(new Statement(64000L,2,"//corsConfig.addAllowedMethod(\"*\");     //放行全部请求"));
		sList.add(new Statement(65000L,0,""));
		sList.add(new Statement(66000L,2,"//是否发送Cookie"));
		sList.add(new Statement(67000L,2,"//config.setAllowCredentials(true);"));
		sList.add(new Statement(68000L,0,""));
		sList.add(new Statement(69000L,2,"//2. 添加映射路径"));
		sList.add(new Statement(70000L,2,"UrlBasedCorsConfigurationSource corsConfigurationSource ="));
		sList.add(new Statement(71000L,4,"new UrlBasedCorsConfigurationSource();"));
		sList.add(new Statement(72000L,2,"corsConfigurationSource.registerCorsConfiguration(\"/**\", config);"));
		sList.add(new Statement(73000L,2,"//返回CorsFilter"));
		sList.add(new Statement(74000L,2,"return new CorsFilter(corsConfigurationSource);"));
		sList.add(new Statement(75000L,1,"}"));
		sList.add(new Statement(76000L,0,""));
		sList.add(new Statement(77000L,0,"}"));

		return WriteableUtil.merge(sList).getContent();
	}
}
